<template>
  <div class="component component-params">
    <form class="text-left">
      <ParamsView ref="paramsView" :params="state" :isRadio="true" :isChoose="false" />
    </form>
    <div class="text-center mt-lg">
      <button class="btn btn-default" type="button" @click="save">保存</button>
    </div>
  </div>
</template>

<script>
/* 服务表单 */
import interService from '@/service/action/inter.action.js'
import { objecter } from '@daelui/dogjs/dist/components.js'
import { Messager } from '@daelui/vdog/dist/components.js'
import ParamsView from './params-shadow.vue'
import interBis from './business/interface.js'

export default {
  components: {
    ParamsView
  },

  data () {
    return {
      // 参数数据
      state: {
        isCollapse: false,
        children: []
      }
    }
  },

  methods: {
    /**
     * @function 保存
    */
    save () {
      // 包装参数
      let params =
        interBis.formatParams(
          interBis.packParams(
            objecter.extend(true, [], this.state.children)
          )
        )

      // 提交数据
      let defer = interService.saveTplParam(params)
      defer.then((result) => {
        if (result.success) {
          Messager.success('保存成功')
        } else {
          Messager.error(result.msg || '保存失败')
        }
      }, function () {
        Messager.error('保存失败')
      })
    },

    /**
     * @function 载入数据
    */
    load () {
      this.state.children = []
      this.checked = null
      // 查询参数
      interService.queryTplParam({})
        .then(result => {
          result = result || {}
          let data = result.data || []
          this.renderInter(data)
        })
    },

    /**
     * @function 渲染参数
     * @param {Array} params
    */
    renderInter (params) {
      /* 解包参数 */
      let myParams = params || []
      if (params.length < 1) {
        params.push({type: 'string', required: 1})
      }
      this.state.children = interBis.unPackParams(
        myParams
      )
    },

    /**
     * @function 获取规则数据
    */
    getState () {
      // 包装参数
      let params =
        interBis.formatParams(
          interBis.packParams(
            objecter.extend(true, [], this.state.children)
          )
        )
      // 选中的参数
      let checked = this.$refs.paramsView.checked || {}
      // 获取匹配的节点
      return interBis.getMatchNode(checked, params)
    }
  }
}
</script>

<style lang="less">

</style>
